
public class Test {
    public static void main(String[] args) {
        // enum和switch的使用
        Weekday day = Weekday.MON;
        switch (day) {
            case MON:
                System.out.println("Monday");
                break;
            case TUE:
                System.out.println("Tuesday");
                break;
            default:
                System.out.println("default case");
                break;
        }
        // enum类型的每个常量在JVM中只有一个唯一实例，所以可以直接用==比较
        Weekday anotherDay = Weekday.MON;
        if (day == anotherDay) {
            System.out.println("Same day");
        }
        // name()方法：返回枚举常量的名称
        System.out.println("Day name: " + day.name());
        // ordinal()方法：返回枚举常量的序号，从0开始
        System.out.println("Day ordinal: " + day.ordinal());

        // 使用带参数的enum
        Color color = Color.RED;
        System.out.println("Color: " + color);
        System.out.println("Color code: " + color.getCode());

    }
}

// 定义的enum类型总是继承自java.lang.Enum，且无法被继承
enum Weekday {
    SUN, MON, TUE, WED, THU, FRI, SAT;
}

// enum自定义属性和方法
enum Color {
    RED(1, "Red Color"), GREEN(2, "Green Color"), BLUE(3, "Blue Color");

    // enum的构造方法要声明为private，字段强烈建议声明为final
    private final String description;
    private final int code;

    private Color(int code, String description) {
        this.description = description;
        this.code = code;
    }

    @Override
    public String toString() {
        return this.description;
    }

    public int getCode() {
        return this.code;
    }
}
// import static enum means: 
// every enum constant is implicitly public static final
// When you write enum Color { RED, GREEN, BLUE; }, it is equivalent to:
// public final class Color extends Enum<Color> {
//     public static final Color RED = new Color("RED", 0);
//     public static final Color GREEN = new Color("GREEN", 1);
//     public static final Color BLUE = new Color("BLUE", 2);
//     private Color(String name, int ordinal) {
//         super(name, ordinal);
//     }
// }    